<?php

namespace App\Http\Controllers;

use App\Models\Classify;
use App\Models\ClassifyTemplate;
use App\Services\ClassifyTemplateService;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

/**
 * 分类目录模板
 * Class ClassifyTemplateController
 * @package App\Http\Controllers
 */
class ClassifyTemplateController extends Controller
{
    public $classifyTemplateService;

    public function __construct(ClassifyTemplateService $classifyTemplateService)
    {
        parent::__construct();
        $this->classifyTemplateService = $classifyTemplateService;
    }

    /**
     * 分类模板列表
     */
    public function index(Request $request)
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->classifyTemplateService->getList($request, $perPage);

        return $this->success($list);
    }

    /**
     * 保存分类模板数据
     */
    public function store(Request $request)
    {
        if (!$this->classifyTemplateService->saveData($request->all())) {
            return $this->error('添加失败！');
        }

        return $this->success('', '添加成功！');
    }

    /**
     * 回收站列表（进行软删除的数据）
     */
    public function recycle(Request $request)
    {
        $data = $this->classifyTemplateService->getRecycles($request);

        return $this->success($data);
    }

    /**
     * 单个软删除
     */
    public function destroy(Request $request)
    {
        $id = $request->input('id');
        if (!ClassifyTemplate::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };
        CommonService::addActionLog('删除分类模板', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 恢复单个数据
     */
    function retrieve(Request $request)
    {
        $id = $request->input('id', 0);
        if (!ClassifyTemplate::query()->where('id', $id)->restore()) {
            return $this->error('恢复失败！');
        }
        CommonService::addActionLog('恢复分类模板', "数据ID:{$id}");

        return $this->success('', '恢复成功');
    }

}
